package com.factory.test.activity;

import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Html;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityWifiBinding;
import com.factory.test.util.WifiAdmin;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * wifi测试
 */
public class WIFIActivity extends BaseTestActivity {

    private ActivityWifiBinding binding;
    private WifiAdmin wifiAdmin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_wifi);

        wifiAdmin = new WifiAdmin(context);
        binding.message.setText("WIFI扫描中...");
        if(!wifiAdmin.isOpen()){
            //Intent panelIntent = new Intent(Settings.Panel.ACTION_WIFI);
            //startActivityForResult(panelIntent, 1);
            wifiAdmin.openWifi();

            //打开wifi后, 1.5秒扫描
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    wifiAdmin.startScan();
                }
            }, 1500);

            //2秒后加载列表
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    wifiAdmin.startScan();
                    List<ScanResult> list = wifiAdmin.getWifiList();
                    if(list !=null && list.size() > 0){
                        initData();
                    }else{
                        handler.postDelayed(scanRunable, 500);
                    }
                }
            }, 2000);
        }else {
            scan();
        }
    }

    private Runnable scanRunable = new Runnable() {
        @Override
        public void run() {
            wifiAdmin.startScan();
            List<ScanResult> list = wifiAdmin.getWifiList();
            if(list == null || list.size() == 0){
                handler.removeCallbacks(this);
                handler.postDelayed(this, 1000);
            }else{
                initData();
            }
        }
    };


    private void scan(){
        wifiAdmin.startScan();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initData();
            }
        }, 3000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1){
            if(wifiAdmin.isOpen()){
                scan();
            }else{
                Intent panelIntent = new Intent(Settings.Panel.ACTION_WIFI);
                startActivityForResult(panelIntent, 1);
            }
        }
    }

    private void initData() {
        binding.mac.setText("MAC: " + sdkService.getWiFiMac());

        List<ScanResult> list = wifiAdmin.getWifiList();
        if (list == null || list.size() == 0) {
            binding.message.setText("WIFI扫描结束");
            return;
        }

        String s = scanResultToString(list);
        binding.message.setText(Html.fromHtml(s));
    }

    public static String scanResultToString(List<ScanResult> list) {
        Collections.sort(list, new Comparator<ScanResult>() {
            @Override
            public int compare(ScanResult o1, ScanResult o2) {
                int wifi1 = WifiManager.calculateSignalLevel(o1.level, 100);
                int wifi2 = WifiManager.calculateSignalLevel(o2.level, 100);
                return wifi2 - wifi1;
            }
        });


        StringBuffer sb = new StringBuffer();
        for (ScanResult scanResult : list) {
            if(scanResult.SSID == null || scanResult.SSID.trim().length() == 0){
                continue;
            }
            int level = WifiManager.calculateSignalLevel(scanResult.level, 100);
            if(level > 90){
                sb.append("<font color='#008000'>")
                        .append("(强度")
                        .append(level)
                        .append(")")
                        .append(scanResult.SSID)
                        .append("</font>")
                        .append("<br>");
                ;
            }else if(level > 80){
                sb.append("<font color='#32CD32'>")
                        .append("(强度")
                        .append(level)
                        .append(")")
                        .append(scanResult.SSID)
                        .append("</font>")
                        .append("<br>");
            }else{
                sb.append("<font color='#98FB98'>")
                        .append("(强度")
                        .append(level)
                        .append(")")
                        .append(scanResult.SSID)
                        .append("</font>")
                        .append("<br>");
            }

        }
        return sb.toString();
    }

}